{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from collections import Counter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Counter({'a': 1, 'b': 1, 'c': 1})\n"
     ]
    }
   ],
   "source": [
    "print(Counter(\"abc\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "c=Counter(\"abc\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "collections.Counter"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "d=dict(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'a': 1, 'b': 1, 'c': 1}\n"
     ]
    }
   ],
   "source": [
    "print(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<itertools.chain at 0x1d2adec2320>"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c.elements()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "mm=list(c.elements())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'b', 'c']"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mm"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "偏函数：partial       特点举例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a b 1 2\n"
     ]
    }
   ],
   "source": [
    "from functools import partial\n",
    "# def foo():\n",
    "def bar(a=1,b=2,c=3,d=4):\n",
    "    print(a,b,c,d)\n",
    "p=partial(bar,'a','b')\n",
    "p(1,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'__closure__', '__get__', '__globals__', '__kwdefaults__', '__code__', '__qualname__', '__module__', '__name__', '__defaults__', '__annotations__'}\n"
     ]
    }
   ],
   "source": [
    "print(set(dir(bar))-set(dir(p)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "unexpected EOF while parsing (<ipython-input-24-44977e4ec775>, line 6)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-24-44977e4ec775>\"\u001b[1;36m, line \u001b[1;32m6\u001b[0m\n\u001b[1;33m    updated=WRAPPER_UPDATES):\u001b[0m\n\u001b[1;37m                             ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m unexpected EOF while parsing\n"
     ]
    }
   ],
   "source": [
    "# 即偏函数相对于正常函数，缺失了一些功能\n",
    "# 补全功能的方法：       update_wrapper\n",
    "def update_wrapper(wrapper,\n",
    "                   wrapped,\n",
    "                   assigned=WRAPPER_ASSIGNMENTS,\n",
    "                   updated=WRAPPER_UPDATES):\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('__module__', '__name__', '__qualname__', '__doc__', '__annotations__')\n"
     ]
    }
   ],
   "source": [
    "import functools \n",
    "print(functools.WRAPPER_ASSIGNMENTS)###   ? ? ?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "from functools import update_wrapper"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a b 1 2\n"
     ]
    }
   ],
   "source": [
    "from functools import partial\n",
    "# def foo():\n",
    "def bar(a=1,b=2,c=3,d=4):\n",
    "    print(a,b,c,d)\n",
    "p=partial(bar,'a','b')\n",
    "p(1,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'__closure__', '__get__', '__globals__', '__kwdefaults__', '__code__', '__qualname__', '__module__', '__name__', '__defaults__', '__annotations__'}\n"
     ]
    }
   ],
   "source": [
    "a=set(dir(bar))-set(dir(p))\n",
    "print(set(dir(bar))-set(dir(p)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "p1=update_wrapper(p,bar)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'__closure__', '__get__', '__globals__', '__kwdefaults__', '__code__', '__defaults__'}\n"
     ]
    }
   ],
   "source": [
    "b=set(dir(bar))-set(dir(p1))\n",
    "print(set(dir(bar))-set(dir(p1)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "update_wrapper修复的函数有:{'__name__', '__module__', '__qualname__', '__annotations__'}\n"
     ]
    }
   ],
   "source": [
    "print(\"update_wrapper修复的函数有:{}\".format(a-b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 为什么修复了一些函数呢？\n",
    "# 那剩下来的函数该如何处理呢？\n",
    "# 修复的部分功能，实际作用如何"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "bar\n"
     ]
    }
   ],
   "source": [
    "print(p1.__name__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "functools.partial(<function bar at 0x000001D2ADEA9840>, 'a', 'b')"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "bar\n",
      "__main__\n",
      "bar\n",
      "{}\n"
     ]
    }
   ],
   "source": [
    "print(p1.__name__)\n",
    "print(p1.__module__)\n",
    "print(p1.__qualname__)\n",
    "print(p1.__annotations__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用装饰器时，对于被装饰函数的name并不容易调用，\n",
    "# 此时update_wrapper的作用就体现出来了\n",
    "# 要点\n",
    "def f2(func):#原版\n",
    "    def inner():\n",
    "    \"\"\"\"\"\"\n",
    "    return inner\n",
    "def f2(func):#改进版\n",
    "    def inner():\n",
    "    \"\"\"\"\"\"\n",
    "    return updtae_wrapper(inner,func)\n",
    "# 后一个参数为模板"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [],
   "source": [
    "? update_wrapper"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-62-d26c01a98bcb>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-62-d26c01a98bcb>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m    Signature:\u001b[0m\n\u001b[1;37m              ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "Signature:\n",
    " update_wrapper(\n",
    "    wrapper,\n",
    "    wrapped,\n",
    "    assigned=('__module__', '__name__', '__qualname__', '__doc__', '__annotations__'),\n",
    "    updated=('__dict__',),\n",
    ")\n",
    "Docstring:\n",
    "Update a wrapper function to look like the wrapped function\n",
    "\n",
    "wrapper is the function to be updated\n",
    "wrapped is the original function\n",
    "assigned is a tuple naming the attributes assigned directly\n",
    "from the wrapped function to the wrapper function (defaults to\n",
    "functools.WRAPPER_ASSIGNMENTS)\n",
    "updated is a tuple naming the attributes of the wrapper that\n",
    "are updated with the corresponding attribute from the wrapped\n",
    "function (defaults to functools.WRAPPER_UPDATES)\n",
    "File:      e:\\python\\anaconda\\envs\\pytorch\\lib\\functools.py\n",
    "Type:      function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
